import pytest
from typing import List
from api.auth.b_client.b_login import LoginApi
from api.biz.base.base_info import BaseClientApi
from common.MySQLUtils import MySQLHelper
from common.fileLoad import load_yaml_file
from common.redisUtil import RedisUtil


def pytest_collection_modifyitems(items: List["Item"]):
    # items对象是pytest收集到的所有用例对象
    for item in items:
        # item就代表了一条用例
        item._nodeid = item._nodeid.encode('utf-8').decode("unicode-escape")


# b端登录
@pytest.fixture(scope='session')
def get_b_client_token():
    response = LoginApi().send()
    print(response.text)
    auth_token = response.json()["data"]
    BaseClientApi.auth_token = auth_token

@pytest.fixture(scope='session')
def get_b_client_token():
    response = LoginApi().send()
    print(response.text)
    auth_token = response.json()["data"]
    BaseClientApi.auth_token = auth_token

@pytest.fixture(scope='session')
def redis_conn():
    redis_util = load_yaml_file(r'/config/redis_util.yml')["biz"]
    redis_util = RedisUtil(host=redis_util["host"], password=redis_util["password"], db=redis_util["db"])
    return redis_util


@pytest.fixture(scope='session')
def mysql_conn():
    db_info = load_yaml_file(r'/config/db_util.yml')["biz"]
    config = {
        "host": db_info["host"],
        "port": db_info["port"],
        "user": db_info["user"],
        "password": db_info["password"],
        "db": db_info["db"],
        "charset": "utf8mb4"
    }
    db = MySQLHelper(config)
    return db


